﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using Toulr.Services;
using Toulr.Util.Ioc;
using Toulr.Models;
using Toulr.Domains.Account;

/// <summary>
/// 普通页面需要继承的页面基类
/// </summary>
public class CommPageBase : Page
{
    public CommPageBase()
    {
        //
        // TODO: Add constructor logic here
        //
        this.Error += new EventHandler(CommPageBase_Error);
    }

    void CommPageBase_Error(object sender, EventArgs e)
    {
        // Get last error from the server
        if (this.Page.GetType().ToString().EndsWith("error_aspx"))
        {
            return;
        }
        Exception exc = Server.GetLastError();
        Server.ClearError();
        PageHelper.Go2Error("PageError:\r\n" + exc.ToString().Substring(0,1024));
    }

    protected override void OnInitComplete(EventArgs e)
    {
        base.OnInitComplete(e);

        if (Request.QueryString["ReturnUrl"] != null && Request.QueryString["ReturnUrl"].ToString().Length > 0)
        {
            PageHelper.ReturnUrl = Request.QueryString["ReturnUrl"].ToString();
        }

        if (!IsPostBack && !PageHelper.IsLanded
            && Request.Cookies["userinfo"] != null
            && !string.IsNullOrEmpty(Request.Cookies["userinfo"].Value))
        {
            IUserContextService service = UnityHelper.GetInstance<IUserContextService>();
            ActionResult<User> actionResult = service.LoginWithCookieIndentyString(Request.Cookies["userinfo"].Value);
            if (actionResult.ResultCode == 0)
            {
                PageHelper.CurrentUser = actionResult.Result;
            }
        }
    }

    StringBuilder scriptBuilder = new StringBuilder();
    public void AddJs2Page(string jsCode)
    {
        scriptBuilder.AppendLine("<script type=\"text/javascript\">" + jsCode + "</script>");
    }

    public void AddClientErrorMessageBox(string title, string msgHtml)
    {
        AddClientErrorMessageBox(title, msgHtml, null);
    }

    public void AddClientErrorMessageBox(string title, string msgHtml, string buttonText = null)
    {
        title = string.IsNullOrEmpty(title) ? "偷乐网提示" : title;
        buttonText = string.IsNullOrEmpty(buttonText) ? "确定" : buttonText;

        msgHtml = "<div class='server_msg'>" + msgHtml + "</div>";
        string template = @"
            $(document).ready(function(){{
                $('<div></div>')
                .html('{0}')
                .dialog({{
		            autoOpen: true,
		            title: '{1}',
                    show: 'slide',
                    modal: true,
                    width: 360,
		            buttons:{{
                            '{2}': function () {{ $(this).dialog('close'); }}
                        }}
                    }});
            }});
            ";

        AddJs2Page(string.Format(template,
            msgHtml.Replace("\"", "\\\"").Replace("'", "\\\'"),
            title.Replace("\"", "\\\""),
            buttonText.Replace("\"", "\\\"")));
    }

    protected override void OnLoadComplete(EventArgs e)
    {
        AjaxPro.Utility.RegisterTypeForAjax(this.GetType());
        AjaxPro.Utility.RegisterTypeForAjax(typeof(CommAjaxHelper));
        //base.OnLoadComplete(e);
        ClientScript.RegisterStartupScript(this.GetType(), "ServerGenScript", scriptBuilder.ToString());

    }
}